package mmc.server.shell.ops.function;

import java.lang.reflect.Constructor;
import java.util.Vector;

import mmc.server.shell.ShellException;
import mmc.server.shell.Util;
import mmc.server.shell.ops.Function;
import mmc.server.shell.ops.Operation;

public class New extends Function {

	Class<?> clasz;
	
    public New(String text) throws ShellException {
        super(text, "new", 1, true);
        
        clasz = arguments.getArguments().remove(0).getOutputType();
    }

    @Override
    public double execute() {
        return Util.assignToDouble(executeComplex());
    }
    
    public Object executeComplex(){
    	Vector<Operation> args = arguments.getArguments();
		
    	
		Constructor<?> con = Util.searchConstructor(clasz, args);
		
		if(con==null)
			throw new RuntimeException("no matching constructor found for "+clasz.getSimpleName());
		
    	return Util.invokeConstructor(con, args);
    }
    
    public Class<?> getOutputType(){
    	return clasz;
    }
    
    public static String getDescription(){
    	return formatHelpString("new(class,args)","invoke constructor");
    }
}
